﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AudioPlayerLib;

namespace SpotIt
{
    /// <summary>
    /// Interaction logic for SpotIt_SettingsWindow.xaml
    /// </summary>
    public partial class SpotIt_SettingsWindow : Window
    {
        AudioFiles theAudioFiles = AudioFiles.Instance;
        Settings settings = Settings.Instance;
        public SpotIt_SettingsWindow()
        {
            InitializeComponent();
            applyValues();
        }

        public void applyValues()
        {
            musicVolumeNumberLabel.Content = (int)(settings.musicVolume * 100);
            soundEffectsVolumeNumberLabel.Content = (int)(settings.soundEffectsVolume * 100);
            buttonBeepVolumeNumberLabel.Content = (int)(settings.buttonBeepVolume * 100);
            
            musicSlider.Value = settings.musicVolume * 100;
            soundEffectsSlider.Value = settings.soundEffectsVolume * 100;
            buttonBeepSlider.Value = settings.buttonBeepVolume * 100;
            
            if (settings.soundOn)
                soundOnRadButton.IsChecked = true;
            else
                soundOffRadButton.IsChecked = true;
            if (settings.musicOn)
                musicOnRadButton.IsChecked = true;
            else
                musicOffRadButton.IsChecked = true;
            if (settings.soundEffectsOn)
                soundEffectsOnRadButton.IsChecked = true;
            else
                soundEffectsOffRadButton.IsChecked = true;
            if (settings.buttonBeepOn)
                beepOnRadButton.IsChecked = true;
            else
                beepOffRadButton.IsChecked = true;
        }
        //
        //Events that fire when any of the off radio buttons are checked
        private void soundOffRadButton_Checked(object sender, RoutedEventArgs e)
        {
            changeAllVolumes(-1); //negative one means to mute all of the volumes
            settings.soundOn = false;
            settings.musicOn = false;
            settings.buttonBeepOn = false;
            settings.soundEffectsOn = false;
            musicSlider.IsEnabled = false;
            musicVolumeNumberLabel.Opacity = .50;
            soundEffectsSlider.IsEnabled = false;
            soundEffectsVolumeNumberLabel.Opacity = .50;
            buttonBeepSlider.IsEnabled = false;
            buttonBeepVolumeNumberLabel.Opacity = .50;

            beepOnRadButton.IsEnabled = false;
            beepOffRadButton.IsEnabled = false;
            musicOnRadButton.IsEnabled = false;
            musicOffRadButton.IsEnabled = false;
            soundEffectsOnRadButton.IsEnabled = false;
            soundEffectsOffRadButton.IsEnabled = false;
        }
        
        private void musicOffRadButton_Checked(object sender, RoutedEventArgs e)
        {
            changeMusicVolume(0);
            settings.musicOn = false;
            //settings.musicVolume = 0;
            musicSlider.IsEnabled = false;
            musicVolumeNumberLabel.Opacity = .50;
            if (!settings.soundEffectsOn && !settings.buttonBeepOn)
            {
                soundOnRadButton.IsEnabled = false;
                soundOffRadButton.IsEnabled = false;
                settings.soundOn = false;
                musicOnRadButton.IsEnabled = true;
            }
        }

        private void soundEffectsOffRadButton_Checked(object sender, RoutedEventArgs e)
        {
            changeSoundEffectsVolume(0);
            settings.soundEffectsOn = false;
            //settings.soundEffectsVolume = 0;
            soundEffectsSlider.IsEnabled = false;
            soundEffectsVolumeNumberLabel.Opacity = .50;
            if (!settings.musicOn && !settings.buttonBeepOn)
            {
                soundOnRadButton.IsEnabled = false;
                soundOffRadButton.IsEnabled = false;
                settings.soundOn = false;
                soundEffectsOnRadButton.IsEnabled = true;
            }
        }

        private void beepOffRadButton_Checked(object sender, RoutedEventArgs e)
        {
            changeButtonBeepVolume(0);
            settings.buttonBeepOn = false;
           //settings.buttonBeepVolume = 0;
            buttonBeepSlider.IsEnabled = false;
            buttonBeepVolumeNumberLabel.Opacity = .50;
            if (!settings.soundEffectsOn && !settings.musicOn)
            {
                soundOnRadButton.IsEnabled = false;
                soundOffRadButton.IsEnabled = false;
                settings.soundOn = false;
                beepOnRadButton.IsEnabled = true;
            }
        }
        //
        //Events that fire when any of the on radio buttons are checked
        private void soundOnRadButton_Checked(object sender, RoutedEventArgs e)
        {
            settings.soundOn = true;
            settings.musicVolume = musicSlider.Value/100;
            settings.soundEffectsVolume = soundEffectsSlider.Value/100;
            settings.buttonBeepVolume = buttonBeepSlider.Value/100;
            changeAllVolumes(0); // will change them values stored in the settings class
            if (musicOnRadButton.IsChecked == true)
            {
                settings.musicOn = true;
                musicSlider.IsEnabled = true;
                musicVolumeNumberLabel.Opacity = 1;
            }
            if (soundEffectsOnRadButton.IsChecked == true)
            {
                settings.soundEffectsOn = true;
                soundEffectsSlider.IsEnabled = true;
                soundEffectsVolumeNumberLabel.Opacity = 1;
            }
            if (beepOnRadButton.IsChecked == true)
            {
                settings.buttonBeepOn = true;
                buttonBeepSlider.IsEnabled = true;
                buttonBeepVolumeNumberLabel.Opacity = 1;
            }
            beepOnRadButton.IsEnabled = true;
            beepOffRadButton.IsEnabled = true;
            musicOnRadButton.IsEnabled = true;
            musicOffRadButton.IsEnabled = true;
            soundEffectsOnRadButton.IsEnabled = true;
            soundEffectsOffRadButton.IsEnabled = true;
        }

        private void musicOnRadButton_Checked(object sender, RoutedEventArgs e)
        {
            settings.musicOn = true;
            musicSlider.IsEnabled = true;
            if (!theAudioFiles.theFiles[0].IsPlaying)
                theAudioFiles.theFiles[0].Play();
            musicVolumeNumberLabel.Opacity = 1;
            if (!soundOnRadButton.IsEnabled)
            {
                soundOnRadButton.IsEnabled = true;
                soundOffRadButton.IsEnabled = true;
            }
            changeMusicVolume(settings.musicVolume);
        }

        private void soundEffectsOnRadButton_Checked(object sender, RoutedEventArgs e)
        {
            settings.soundEffectsOn = true;
            soundEffectsSlider.IsEnabled = true;
            soundEffectsVolumeNumberLabel.Opacity = 1;
            if (!soundOnRadButton.IsEnabled)
            {
                soundOnRadButton.IsEnabled = true;
                soundOffRadButton.IsEnabled = true;
            }
            changeSoundEffectsVolume(settings.soundEffectsVolume);
        }

        private void beepOnRadButton_Checked(object sender, RoutedEventArgs e)
        {
            settings.buttonBeepOn = true;
            buttonBeepSlider.IsEnabled = true;
            buttonBeepVolumeNumberLabel.Opacity = 1;
            if (!soundOnRadButton.IsEnabled)
            {
                soundOnRadButton.IsEnabled = true;
                soundOffRadButton.IsEnabled = true;
            }
            changeButtonBeepVolume(settings.buttonBeepVolume);
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        //
        //Fuctions to that fire when a slider value is changed
        private void musicSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            settings.musicVolume = musicSlider.Value/100;
            musicVolumeNumberLabel.Content = e.NewValue.ToString();
            changeMusicVolume(settings.musicVolume);
        }

        private void soundEffectsSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            soundEffectsVolumeNumberLabel.Content = e.NewValue.ToString();
            settings.soundEffectsVolume = soundEffectsSlider.Value / 100;
            changeSoundEffectsVolume(settings.soundEffectsVolume);
        }

        private void buttonBeepSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            settings.buttonBeepVolume = buttonBeepSlider.Value / 100;
            buttonBeepVolumeNumberLabel.Content = e.NewValue.ToString();
            changeButtonBeepVolume(settings.buttonBeepVolume);
        }

        public void changeMusicVolume(double newValue)
        {
            theAudioFiles.theFiles[0].changeVolume(newValue);
            theAudioFiles.theFiles[1].changeVolume(newValue);
        }
        public void changeButtonBeepVolume(double newValue)
        {
            theAudioFiles.theFiles[2].changeVolume(newValue);
            theAudioFiles.theFiles[3].changeVolume(newValue);
        }

        public void changeSoundEffectsVolume(double newValue)
        {
            //Currently no sound effects are implemented
        }

        public void changeAllVolumes(double newValue)
        {
            if (newValue == -1)
            {
                for (int count = 0; count < theAudioFiles.getSize(); count++)
                    theAudioFiles.theFiles[count].changeVolume(newValue);
            }
            else
            {
                if(musicOnRadButton.IsChecked == true)
                    changeMusicVolume(settings.musicVolume);
                if(beepOnRadButton.IsChecked == true)
                    changeButtonBeepVolume(settings.buttonBeepVolume);
                if(soundEffectsOnRadButton.IsChecked == true)
                    changeSoundEffectsVolume(settings.soundEffectsVolume);
            }
        }

        private void settingsWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            settings.saveSettings();
        }


    }
}
